package com.ym.jiujiangeyou;

import java.util.ArrayList;
import java.util.List;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.weibo.TencentWeibo;

import com.ym.jiujiangeyou.app.AppManager;
import com.ym.jiujiangeyou.app.MyApplication;
import com.ym.jiujiangeyou.bitmap.BitmapUtil;
import com.ym.jiujiangeyou.bitmap.SimpleImageLoad;
import com.ym.jiujiangeyou.database.SceneryService;
import com.ym.jiujiangeyou.entity.Guide;
import com.ym.jiujiangeyou.entity.Scenery;
import com.ym.jiujiangeyou.ext.SatelliteMenu;
import com.ym.jiujiangeyou.ext.SatelliteMenu.SateliteClickedListener;
import com.ym.jiujiangeyou.ext.SatelliteMenuItem;
import com.ym.jiujiangeyou.service.PicUrlService;
import com.ym.jiujiangeyou.ui.DialogView;
import com.ym.jiujiangeyou.util.Constants;
import com.ym.jiujiangeyou.util.FileManager;
import com.ym.jiujiangeyou.util.Util;
import com.ym.jiujiangeyou.weibo.sdk.BaseType;
import com.ym.jiujiangeyou.weibo.sdk.ShareActivity;
import com.ym.jiujiangeyou.weibo.sdk.ShareItem;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class SceneryDetails extends Activity implements OnTouchListener,
		OnClickListener, SateliteClickedListener {

	private final static int QZONE = 1;
	private final static int SINA_ID = 2;
	private final static int WECHAT_ID = 3;
	private final static int WECHAT_MOM_ID = 4;
	private final static int TENCENT_ID = 5;
	private TextView title, tv_opentime, tv_phonenum, tv_address,
			pop_topic_button, pop_collect_button, pop_share_button;
	private LinearLayout back_layou, linearlayout;
	private RelativeLayout rel_introduct, rel_traffic, rel_prompt, rel_address,
			rel_phone;
	private LayoutInflater inflater;
	private View popuView;
	private WindowManager windowManager;
	private WindowManager.LayoutParams lp;
	private ScrollView scroll;
	private Scenery scenery;
	private Guide guide;
	private int width;
	private int height;
	Dialog dialog;
	Toast toast;
	SimpleImageLoad simpleImageLoad;
	ImageView img_collect, scenery_pic;
	MyApplication application = null;

	private BaseType.ListItem mTypeItem = new BaseType.ListItem();
	private BaseType.InfoItemEx mInfoItem = new BaseType.InfoItemEx();

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case Constants.SCENERY_COLLECT_SUCCESS:
				img_collect.setVisibility(View.GONE);
				img_collect.setClickable(false);
				dialog.dismiss();
				toast = DialogView.popToast(SceneryDetails.this,
						R.string.detail_collect_success);
				toast.show();
				break;
			case Constants.SCENERY_COLLECT_ERROR:
				dialog.dismiss();
				toast = DialogView.popToast(SceneryDetails.this,
						R.string.detail_collect_error);
				toast.show();
				break;
			case Constants.SCENERY_COLLECT_ALREADY:
				img_collect.setVisibility(View.GONE);
				img_collect.setClickable(false);
				/*
				 * dialog.dismiss(); toast =
				 * DialogView.popToast(SceneryDetails.this,
				 * R.string.detail_collect_already); toast.show();
				 */
				break;
			case Constants.SCENERY_COLLECT_NOT:
				img_collect.setVisibility(View.VISIBLE);
				img_collect.setOnClickListener(SceneryDetails.this);
				break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		application = MyApplication.getInstance();
		super.setContentView(R.layout.scenery_listitem_details);

		// windowManager.addView(popuView, lp);

		// Bundle bundle = this.getIntent().getExtras();
		Intent intent = getIntent();
		scenery = (Scenery) intent.getSerializableExtra("scenery");
		guide = (Guide) intent.getSerializableExtra("guide");

		/**
		 * 启动service获取图片连接
		 */
		Intent intent2 = new Intent(this, PicUrlService.class);
		intent2.putExtra("id", scenery.getId());
		intent2.putExtra("flag", "scenery");
		intent2.putExtra("url", Constants.GET_SCENERY_PIC_URL);
		startService(intent2);

		initResouce();
		// System.out.println("--------scenery----"+scenery.toString()) ;
		// System.out.println("--------guide----"+guide.toString()) ;

		title.setText(scenery.getName().toString());
		tv_opentime.setText(guide.getOpenTime().toString());
		tv_phonenum.setText(guide.getPhoneNumber().toString());
		tv_address.setText(scenery.getAddress().toString());
	}

	public void initResouce() {

		title = (TextView) this.findViewById(R.id.title_text);
		back_layou = (LinearLayout) this.findViewById(R.id.title_back);
		tv_phonenum = (TextView) this.findViewById(R.id.tv_phonenum);
		tv_opentime = (TextView) this.findViewById(R.id.tv_opentime);
		tv_address = (TextView) this.findViewById(R.id.tv_address);
		rel_introduct = (RelativeLayout) this.findViewById(R.id.rel_introduct);
		rel_traffic = (RelativeLayout) this.findViewById(R.id.rel_traffic);
		rel_prompt = (RelativeLayout) this.findViewById(R.id.rel_prompt);
		rel_address = (RelativeLayout) this.findViewById(R.id.rel_address);
		rel_phone = (RelativeLayout) this.findViewById(R.id.rel_phone);
		linearlayout = (LinearLayout) this.findViewById(R.id.linearlayout);
		img_collect = (ImageView) findViewById(R.id.img_collect);
		scenery_pic = (ImageView) findViewById(R.id.scenery_pic);

	//	simpleImageLoad = new SimpleImageLoad(this);
		if (scenery.getImageurl() != null
				&& FileManager.getBitmap(scenery.getImageurl()) != null) {
			scenery_pic
					.setImageBitmap(FileManager.getBitmap(scenery.getImageurl()));
		} else {
			simpleImageLoad = new SimpleImageLoad(this);
			simpleImageLoad.DisplayImage(scenery.getImageurl(), scenery_pic);
		}

		/*
		 * // 等到屏幕的大小 width =
		 * this.getWindowManager().getDefaultDisplay().getWidth(); height =
		 * this.getWindowManager().getDefaultDisplay().getHeight(); height =
		 * (height / 4) + 50; LinearLayout.LayoutParams layoutParams =
		 * (LinearLayout.LayoutParams) linearlayout .getLayoutParams();
		 * layoutParams.width = width; layoutParams.height = height;
		 * linearlayout.setLayoutParams(layoutParams); LinearLayout.LayoutParams
		 * mParams = new LinearLayout.LayoutParams(
		 * LinearLayout.LayoutParams.WRAP_CONTENT,
		 * LinearLayout.LayoutParams.WRAP_CONTENT); mParams.setMargins(10, 0,
		 * 10, 0) ; ImageView iv = new ImageView(this);
		 * iv.setLayoutParams(mParams);
		 */

		/*
		 * iv.setImageBitmap(BitmapUtil.zoomImage(
		 * BitmapFactory.decodeResource(getResources(), R.drawable.sd), width,
		 * height));
		 */
		// simpleImageLoad.setSize(width, height) ;
	//	simpleImageLoad.DisplayImage(scenery.getImageurl(), scenery_pic);
		// linearlayout.addView(iv);

		isCollectImgShow();

		SatelliteMenu satellite_menu = (SatelliteMenu) findViewById(R.id.satellite_menu);
		List<SatelliteMenuItem> items = new ArrayList<SatelliteMenuItem>();
		items.add(new SatelliteMenuItem(QZONE, R.drawable.logo_qzone));
		items.add(new SatelliteMenuItem(WECHAT_ID, R.drawable.logo_wechat));
		items.add(new SatelliteMenuItem(WECHAT_MOM_ID,
				R.drawable.logo_wechatmoments));
		items.add(new SatelliteMenuItem(SINA_ID, R.drawable.logo_sina));
		items.add(new SatelliteMenuItem(TENCENT_ID,
				R.drawable.logo_tencentweibo));
		satellite_menu.addItems(items);
		satellite_menu.setOnItemClickedListener(this);

		/*
		 * scroll = (ScrollView) this.findViewById(R.id.details_scroll);
		 * inflater = LayoutInflater.from(this); popuView =
		 * inflater.inflate(R.layout.scenery_details_popuview, null);
		 * 
		 * pop_topic_button = (TextView) popuView
		 * .findViewById(R.id.pop_topic_button); pop_collect_button = (TextView)
		 * popuView .findViewById(R.id.pop_collect_button); pop_share_button =
		 * (TextView) popuView .findViewById(R.id.pop_share_button);
		 * 
		 * lp = new WindowManager.LayoutParams(LayoutParams.FILL_PARENT,
		 * LayoutParams.WRAP_CONTENT,
		 * WindowManager.LayoutParams.TYPE_APPLICATION, // 设置为无焦点状态
		 * WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
		 * WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, // 没有边界 // 半透明效果
		 * PixelFormat.TRANSLUCENT); lp.gravity = Gravity.BOTTOM;
		 * scroll.setOnTouchListener(this); lp.windowAnimations =
		 * R.style.scenery_popu_view_style; windowManager = (WindowManager)
		 * getSystemService(WINDOW_SERVICE);
		 */

		back_layou.setOnClickListener(this);
		rel_introduct.setOnClickListener(this);
		rel_prompt.setOnClickListener(this);
		rel_traffic.setOnClickListener(this);
		rel_address.setOnClickListener(this);
		rel_phone.setOnClickListener(this);
		scenery_pic.setOnClickListener(this);
		// img_collect.setOnClickListener(this) ;
		// pop_topic_button.setOnClickListener(this);
		// pop_collect_button.setOnClickListener(this);
		// pop_share_button.setOnClickListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_MOVE) {
			popuView.setVisibility(View.GONE);
		} else {
			popuView.setVisibility(View.VISIBLE);
		}
		return false;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		// windowManager.removeView(popuView);
		super.onDestroy();
	}

	/**
	 * 
	 * @retur void
	 * @Description: 查询是否搜藏过
	 */
	public void isCollectImgShow() {
		new Thread(new Runnable() {
			Message m = new Message();
			SceneryService s = new SceneryService(SceneryDetails.this);

			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean flag = s.queryByName(scenery.getName());
				if (!flag) {
					m.what = Constants.SCENERY_COLLECT_NOT;
				} else {
					m.what = Constants.SCENERY_COLLECT_ALREADY;
				}
				handler.sendMessage(m);
			}
		}).start();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.rel_phone:
			Intent intent4 = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ guide.getPhoneNumber()));
			startActivity(intent4);
			break;
		case R.id.title_back:
			// Intent intent = new Intent(SceneryDetails.this,
			// SceneryListActivity.class) ;
			// Intent intent = new Intent(SceneryDetails.this,
			// SightListActivity.class) ;
			// startActivity(intent) ;
			onBackPressed();
			break;

		case R.id.rel_introduct:
			Intent intent = new Intent(this, SceneryIntroduct.class);
			Bundle bundle = new Bundle();
			bundle.putString("introduct", scenery.getIntroduction());
			// bundle.putString("ticket", guide.getTicket()) ;
			intent.putExtras(bundle);
			startActivity(intent);
			break;
		case R.id.rel_prompt:
			Intent intent3 = new Intent(this, SceneryPrompt.class);
			Bundle bundle3 = new Bundle();
			bundle3.putString("prompt", guide.getPrompt());
			intent3.putExtras(bundle3);
			startActivity(intent3);
			break;
		case R.id.rel_traffic:
			Intent intent1 = new Intent(this, SceneryTraffic.class);
			Bundle bundle1 = new Bundle();
			bundle1.putString("traffic", guide.getTraffic());
			intent1.putExtras(bundle1);
			startActivity(intent1);
			break;
		case R.id.rel_address:
			Intent intent2 = new Intent(this, SceneryMapActivity.class);
			Bundle bundle2 = new Bundle();
			bundle2.putDouble("sLat", scenery.getLatitude());// scenery.getLatitude()
			bundle2.putDouble("sLon", scenery.getLongitude());// scenery.getLongitude()
			bundle2.putString("sName", scenery.getName());
			bundle2.putString("flag", "scenery");
			intent2.putExtras(bundle2);
			startActivity(intent2);
			break;
		case R.id.img_collect:
			dialog = DialogView
					.popDialog(this, R.string.detail_collect_loading);
			/*
			 * Toast.makeText(this, "pop_collect_button", Toast.LENGTH_SHORT)
			 * .show();
			 */
			dialog.show();
			new Thread(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Message message = new Message();
					SceneryService service = new SceneryService(
							SceneryDetails.this);
					// boolean flag = service.queryByName(scenery.getName());
					// if (!flag) {
					long i = service.insetSceneryToBase(scenery, guide);
					if (i > 0) {
						message.what = Constants.SCENERY_COLLECT_SUCCESS;
					} else {
						message.what = Constants.SCENERY_COLLECT_ERROR;
					}
					/*
					 * //} else { message.what =
					 * Constants.SCENERY_COLLECT_ALREADY; }
					 */

					handler.sendMessage(message);
				}
			}).start();
			break;
		case R.id.scenery_pic:
			// List<String> urls = application.getUrls() ;
			List<String> urls = application.getSceneryUrlsCache(Util
					.replaceUrlWithPlus(Constants.GET_SCENERY_PIC_URL
							+ scenery.getId()));
			// System.out.println("-------------urls---------------"+urls.size())
			// ;
			Intent intent5 = null;
			if (urls != null && !urls.isEmpty()) {
				System.out.println("-------------1--------------");
				application.setUrls(urls);
				intent5 = new Intent(SceneryDetails.this,
						PictureBrowerActivity.class);
				startActivity(intent5);
				System.out.println("-------------2--------------");
			} else {
				if (scenery.getImageurl() != null
						&& FileManager.getBitmap(scenery.getImageurl()) != null) {
					System.out.println("-------------3--------------");
					intent5 = new Intent(SceneryDetails.this,
							PicDetailActivity.class);
					intent5.putExtra("imageurl", scenery.getImageurl());
					startActivity(intent5);
				} else {
					System.out.println("-------------4--------------");
					Toast.makeText(this, R.string.http_connect_error,
							Toast.LENGTH_SHORT).show();
				}
			}

			break;
		/*case R.id.pop_topic_button:
			Toast.makeText(this, "pop_topic_button", Toast.LENGTH_SHORT).show();
			break;
		case R.id.pop_share_button:
			Toast.makeText(this, "pop_share_button", Toast.LENGTH_SHORT).show();
			break;*/
		}
	}

	/**
	 * 
	 * @retur void
	 * @Description: 分享到sina
	 */
	private void shareToSina() {
		// LAroundApplication.getInstance().onEvent("UMID0008");
		ShareItem.clear();

		String imageURL = mInfoItem.getImageURL(0);
		if (scenery != null) {
			ShareItem.setText(scenery.getName() + "     ---"
					+ scenery.getAddress());
		} else {
			ShareItem.setText(mInfoItem.mContent);
		}
		// if (imageURL != null){
		// ShareItem.setImageUrl(imageURL);
		// ShareItem.setImageUrl(scenery.getImageurl());
		// String sharPath = fileCache.getSavePath(imageURL);
		ShareItem.setShareImagePath(FileManager.getImagePath(scenery
				.getImageurl()));
		System.out.println("--------000001111----------"
				+ scenery.getImageurl());
		System.out.println("--------11110000----------"
				+ FileManager.getImagePath(scenery.getImageurl()));
		// }
		ShareItem.setPlatform(SinaWeibo.NAME);
		goShareActivity();
	}

	/**
	 * 
	 * @retur void
	 * @Description: 分享到tencent微博
	 */
	private void shareToTencent() {
		ShareItem.clear();

		// String imageURL = mInfoItem.getImageURL(0);
		if (scenery != null) {
			ShareItem.setTitle(scenery.getName());
			ShareItem.setText(scenery.getAddress());
		} else {
			ShareItem.setTitle(mInfoItem.mTitle);
			// ShareItem.setTitleUrl("http://blog.csdn.net/lancees");
			ShareItem.setText(mInfoItem.mContent);
		}
		// if (imageURL != null){
		// String sharPath = fileCache.getSavePath(imageURL);
		ShareItem.setImagePath(FileManager.getImagePath(scenery.getImageurl()));
		ShareItem.setShareImagePath(FileManager.getImagePath(scenery
				.getImageurl()));
		// }
		ShareItem.setPlatform(TencentWeibo.NAME);
		goShareActivity();
	}

	/**
	 * 
	 * @retur void
	 * @Description: go to分享activity
	 */
	private void goShareActivity() {

		Intent intent = new Intent();
		intent.setClass(this, ShareActivity.class);
		startActivity(intent);
	}

	/*
	 * 按返回键是自动调用
	 */
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		// overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
		// this.finish();
	}

	@Override
	public void eventOccured(int id) {
		// TODO Auto-generated method stub
		switch (id) {
		case SINA_ID:
			shareToSina();
			break;
		case TENCENT_ID:
			shareToTencent();
			break;
		case WECHAT_ID:
			Toast.makeText(this, "开发中ing", Toast.LENGTH_SHORT).show();
			break;
		case WECHAT_MOM_ID:
			Toast.makeText(this, "开发中ing", Toast.LENGTH_SHORT).show();
			break;
		case QZONE:
			Toast.makeText(this, "开发中ing", Toast.LENGTH_SHORT).show();
			break;
		}
	}

}
